<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>将字符串解析成多个变量</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.ord.html">« ord</a></li>
      <li style="float: right;"><a href="function.print.html">print »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.strings.html">字符串 函数</a></li>
    <li>将字符串解析成多个变量</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.parse-str" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">parse_str</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">parse_str</span> &mdash; <span class="dc-title">将字符串解析成多个变量</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.parse-str-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>parse_str</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$string</code></span>, <span class="methodparam"><span class="type">array</span> <code class="parameter reference">&$result</code></span>): <span class="type"><span class="type void">void</span></span></div>

  <p class="para rdfs-comment">
   如果 <code class="parameter">string</code> 是 URL 传递入的查询字符串（query string），则将它解析为变量并设置到当前作用域（如果提供了 <code class="parameter">result</code> 则会设置到该数组里 ）。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.parse-str-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">string</code></dt>

     <dd>

      <p class="para">
       输入的字符串。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">result</code></dt>

     <dd>

      <p class="para">
       如果设置了第二个变量 <code class="parameter">result</code>，
       变量将会以数组元素的形式存入到这个数组，作为替代。
      </p>

      <div class="warning"><strong class="warning">警告</strong>
       <p class="para">
        极度<em class="emphasis">不建议</em>在没有 <code class="parameter">result</code> 参数的情况下使用此函数，
        并且在 PHP 7.2 中将<em class="emphasis">废弃</em>不设置参数的行为。PHP 8.0.0
        起，<code class="parameter">result</code> 参数是<em class="emphasis">强制的</em>。
       </p>
      </div>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.parse-str-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   没有返回值。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.parse-str-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
     <tr>
        <td>8.0.0</td>
        <td>
         <code class="parameter">result</code> 是必须项。
        </td>
       </tr>

      <tr>
       <td>7.2.0</td>
       <td>
        不带第二个参数的情况下使用 <span class="function"><strong>parse_str()</strong></span> 会产生 
        <strong><code>E_DEPRECATED</code></strong> 警告。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.parse-str-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4794">
    <p><strong>示例 #1 <span class="function"><strong>parse_str()</strong></span> 的使用</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$str&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"first=value&amp;arr[]=foo+bar&amp;arr[]=baz"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;推荐用法<br /></span><span style="color: #0000BB">parse_str</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$output</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$output</span><span style="color: #007700">[</span><span style="color: #DD0000">'first'</span><span style="color: #007700">];&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;value<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$output</span><span style="color: #007700">[</span><span style="color: #DD0000">'arr'</span><span style="color: #007700">][</span><span style="color: #0000BB">0</span><span style="color: #007700">];&nbsp;</span><span style="color: #FF8000">//&nbsp;foo&nbsp;bar<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$output</span><span style="color: #007700">[</span><span style="color: #DD0000">'arr'</span><span style="color: #007700">][</span><span style="color: #0000BB">1</span><span style="color: #007700">];&nbsp;</span><span style="color: #FF8000">//&nbsp;baz<br /><br />//&nbsp;不建议这么用<br /></span><span style="color: #0000BB">parse_str</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$first</span><span style="color: #007700">;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;value<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">];&nbsp;</span><span style="color: #FF8000">//&nbsp;foo&nbsp;bar<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">];&nbsp;</span><span style="color: #FF8000">//&nbsp;baz<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   由于 PHP 的变量名不能带「点」和「空格」，所以它们会被转化成下划线。
   用本函数带 <code class="parameter">result</code> 参数，也会应用同样规则到数组的键名。
   <div class="example" id="example-4795">
    <p><strong>示例 #2 <span class="function"><strong>parse_str()</strong></span> 名称改写</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />parse_str</span><span style="color: #007700">(</span><span style="color: #DD0000">"My&nbsp;Value=Something"</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$My_Value</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;Something<br /><br /></span><span style="color: #0000BB">parse_str</span><span style="color: #007700">(</span><span style="color: #DD0000">"My&nbsp;Value=Something"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$output</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$output</span><span style="color: #007700">[</span><span style="color: #DD0000">'My_Value'</span><span style="color: #007700">];&nbsp;</span><span style="color: #FF8000">//&nbsp;Something<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p> 
 </div>

 
 <div class="refsect1 notes" id="refsect1-function.parse-str-notes">
  <h3 class="title">注释</h3>

  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    所有创建的变量(或者在设置第二个参数的情况下，返回数组里的值)，
    都已经 <span class="function"><a href="function.urldecode.html" class="function">urldecode()</a></span> 了。
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    要获取当前的 <code class="literal">QUERY_STRING</code>，可以使用
    <var class="varname"><a href="reserved.variables.server.html" class="classname">$_SERVER['QUERY_STRING']</a></var> 变量。
    所以你可能想要阅读
    <a href="language.variables.external.html" class="link">来自 PHP 之外的变量</a>这个章节。
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.parse-str-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.parse-url.html" class="function" rel="rdfs-seeAlso">parse_url()</a> - 解析 URL，返回其组成部分</span></li>
    <li class="member"><span class="function"><a href="function.pathinfo.html" class="function" rel="rdfs-seeAlso">pathinfo()</a> - 返回文件路径的信息</span></li>
    <li class="member"><span class="function"><a href="function.http-build-query.html" class="function" rel="rdfs-seeAlso">http_build_query()</a> - 生成 URL-encode 之后的请求字符串</span></li>
    <li class="member"><span class="function"><a href="function.urldecode.html" class="function" rel="rdfs-seeAlso">urldecode()</a> - 解码已编码的 URL 字符串</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>